package com.tajiang.business.order.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.tajiang.business.R;
import com.tajiang.business.common.activity.BaseActivity;
import com.tajiang.business.common.http.BaseResponse;
import com.tajiang.business.common.http.HttpHandler;
import com.tajiang.business.common.http.HttpResponseListener;

import com.tajiang.business.common.utils.UserUtils;
import com.tajiang.business.model.Store;
import com.tajiang.business.model.User;
import com.tajiang.business.order.adapter.StoreAdapter;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.List;

import butterknife.BindView;

/**
 * 选择食堂
 * Created by work on 2016/6/24.
 */
public class ChooseMessActivity extends BaseActivity implements HttpResponseListener {

    @BindView(R.id.rv_store)
    RecyclerView rvStore;
    @BindView(R.id.tv_school_name)
    TextView tvSchoolName;

    @Override
    protected void initTopBar() {
        setTitle(R.string.msg_choose_mess);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_choose_mess);
    }

    @Override
    protected void initData() {
        User user = UserUtils.getUser();
        tvSchoolName.setText(user.getSchoolName());
        new HttpHandler(this).getStores(user.getSchoolId());
    }

    @Override
    public void onSuccess(BaseResponse response) {
        List<Store> storeList = (List<Store>) response.getData();
        StoreAdapter adapter = new StoreAdapter(storeList, new StoreAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(List<Store> storeList, int position) {
                Bundle mBundle = new Bundle();
                mBundle.putString("store_name", storeList.get(position).getName());
                mBundle.putString("store_id", storeList.get(position).getId());
                ChooseMessActivity.this.setResult(RESULT_OK, ChooseMessActivity.this.getIntent().putExtras(mBundle));
                ChooseMessActivity.this.finish();
            }
        });
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        rvStore.setLayoutManager(layoutManager);
        rvStore.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).color(android.R.color.transparent).size(8).build());
        rvStore.setAdapter(adapter);
    }

    @Override
    public void onFailed(BaseResponse response) {

    }

    @Override
    public void onFinish() {

    }
}
